class Result < ActiveRecord::Base
  belongs_to :work
  belongs_to :scan
    #评论倍值
    E_C = 50
   #收藏倍值
    E_S = 100
   #弹幕倍值
    E_D = 0.5

  def get_ranking_point
    clicks_point = clicks.to_f * click_revised
    comment_point = comments.to_f * comment_revised
    format("%.2f", (clicks_point + comment_point + stows * E_S) * space_fix).to_f
  end

  #娱乐版&youku|tudou修正值
  def space_fix
    if @@space_fix_cache
        if @@space_fix_cache[work.wid]
          @@space_fix_cache[work.wid]
        else
           if ([4, 6, 19, 20, 21, 22, 25, 26].include? work.ac_type_id) && !work.youku_and_tudou.empty?
             @@space_fix_cache[work.wid] = 0.5
           else
             @@space_fix_cache[work.wid] = 1
           end
        end
    else
      if ([4, 6, 19, 20, 21, 22, 25, 26].include? work.ac_type_id) && !work.youku_and_tudou.empty?
        0.5
      else
        1
      end
    end
  end
  
  #点击Part修正
  def self.get_click_revised parts, simple = true
    simple ? format("%.2f", 2.0 / (parts + 1)).to_f : 1.0 / parts
  end

  def click_revised simple = true
    Result.get_click_revised work.part_count, simple
  end
  
  #评论修正
  def self.get_comment_revised fixed_clicks, comments
    if comments <= 0
      0.0
    else
      format("%.2f", E_C * fixed_clicks.to_f / (fixed_clicks.to_f + comments.to_f * E_C)).to_f
    end
  end

  def comment_revised
    Result.get_comment_revised clicks * click_revised, comments
  end
  
  #时间修正(不用)
  def self.get_date_revised date, std_date
    begin
      if std_date && date.to_datetime.compare_with_coercion(std_date + ' 00:00:00') == -1
        E_DATE
      else
        1.0
      end
    rescue
      1.0
    end
  end
  
  def self.point_str point
    point_s = point.to_s
    fushu = point_s.first == '-' ? true : false
    size = point_s.size - ( fushu ? 1 : 0 )
    if size <= 3
      point_s
    else
      str = ""
      point_s.delete('-').split('').each_with_index do |ch, i|
        str << "," if i > 0 && (size - i) % 3 == 0
        str << ch
      end
      fushu ? '-' + str : str
    end
  end

  #修复弹幕数据用
  def fix_danmu
    unless self.work.author_id
      self.work.get_info false
    end
    print "====#{work.wid} #{danmu} => "
    i = 0
    while i += 1
      page = Hpricot.XML(open("http://#{BILI_PATH}/plus/search.php?keyword=#{URI.encode top20name}&typeid=#{self.work.ac_type.page_code}&amid=#{self.work.author.mid}&PageNo=#{i}"))
      items = page.search("div[@class=item]")
      if items.empty?
        print "not found\n"
        break
      end
      flag = false
      page.search("div[@class=item]").each do |item|
        id_uri = item.search("h3").first.search("a").first.get_attribute("href")
        if %[http://#{BILI_PATH}/video/av#{self.work.wid}/] == id_uri || %[http://#{BILI_PATH}/plus/view.php?aid=#{self.work.wid}] == id_uri
          if item.search("span")[3].inner_text == " "
            self.update_attribute :danmu, work.flash_vars.inject(0){|re, item| re += item.get_danmu_count}
            p self.danmu
          else
            print %!#{item.search("span")[3].inner_text}\n!
            self.update_attribute :danmu, item.search("span")[3].inner_text.to_i
          end
          flag = true
          break
        end
      end
      break if flag
    end
  end

  def top20name n=19
    Iconv.iconv('GB18030//IGNORE', 'UTF-8//IGNORE', self.work.name[0..n]).first
  rescue
    top20name n-1
  end
end
